class SessionMgr {
    constructor() {
        this._id = 1;
        this.sessions = {};
        this.sessionsByUid = {};
        this.sessionsByPid = {};
    }

    create(uId) {
        let session = {
            id: this._id,
            uId: uId,
            pId: null,
            player: null
        };
        this.sessions[this._id] = session;
        this.sessionsByUid[uId] = session;
        this._id += 1;
        return session;
    }

    get(id) {
        return this.sessions[id];
    }

    getByUid(uId) {
        let session = this.sessionsByUid[uId];
        return session;
    }



    del(id) {
        let session = this.sessions[id];
        delete this.sessions[session.id];
        delete this.sessionsByUid[session.uId];
        if (session.pId) {
            delete this.sessionsByPid[session.pId];
        }
    }

    setPlayer(uId, player) {
        let session = this.sessionsByUid[uId];
        if (session.pId) {
            delete this.sessionsByPid[session.pId];
        }
        session.pId = player.id;
        session.player = player;
        this.sessionsByPid[player.id] = session;
        return session;
    }

    getByPid(pId) {
        let session = this.sessionsByPid[pId];
        return session;
    }

}

let sessionMgr = new SessionMgr();

module.exports = sessionMgr;